<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <div style="position:absolute;right:0;top:3px">
              <table>
                  <tr>
                      <td>
                          <el-button @click="btnAdd" icon="el-icon-plus" v-has="'PADD'">新增</el-button>
                      </td>
                      <td>
                          <el-button @click="btnDelete" icon="el-icon-delete" v-has="'PDELETE'">删除</el-button>
                      </td>
                      <td>
                          <el-button @click="SaveRecord" icon="el-icon-document">保存</el-button>
                      </td>
                      <td>
                          <el-button @click="RecordExport" icon="el-icon-upload2" v-has="'PEXPORT'">导出</el-button>
                      </td>
                  </tr>
              </table>
          </div>
      </div>
      <div class="pagecenter">
          <!-- <div class="middle-center" style="height:100%;"> -->
          <rm-layout-right splitcolor="#F8F8FF" movingcolor="	#778899" :splitwidth="5" :fixsize="420" :minfixsize="80">
              <vxe-table :loading="loading" :data.sync="tableData" :cell-class-name="cellClassName" :row-class-name="rowClassName" @current-change="currentChangeEvent" border fit :mouse-config="{selected: true}" :edit-config="{trigger: 'click', mode: 'cell',showStatus: true,showIcon:false,activeMethod: activeCellMethod }" @edit-actived="CellActive" :keyboard-config="keyConfig" height="auto" header-align="center" @edit-closed="EditChange" highlight-current-row highlight-hover-row ref="xTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" @edit-disabled="editDisabledEvent">
                  <vxe-table-column type="index" title="NO." width="50" align="center"></vxe-table-column>
                  <vxe-table-column field="germkind" title="分类代码" width="100" sortable :edit-render="{name:'input',autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="germkindname" title="分类名称" width="130" :edit-render="{name:'input',autoselect: true}" sortable>
                  </vxe-table-column>
                  <vxe-table-column field="kindgroup" title="所属大类" width="100" sortable :edit-render="{name:'select', options: $opt.gramOption, autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="kindname_en" title="英文名称" width="100" sortable :edit-render="{name:'input',autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="germmedgroup" title="默认药敏组合" width="100" sortable align="center" :edit-render="{name:'select',options: germmedgroupOption}"></vxe-table-column>
                  <vxe-table-column field="inputcode" title="录入码" width="100" sortable :edit-render="{name:'input',autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="seq" title="顺序号" width="70" sortable :edit-render="{name:'input',autoselect: true}" align="center"></vxe-table-column>
              </vxe-table>
              <template v-slot:fix>
                  <vxe-table :loading="loading" :data.sync="rXTableData" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row ref="rXTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini">
                      <vxe-table-column type="index" title="NO." width="40" align="center" empty-text="暂无菌属数据，请前往LIS811细菌字典维护。"></vxe-table-column>
                      <vxe-table-column field="rpt_itemcode" title="菌属代码" width="80" sortable :edit-render="{name:'input',autoselect: true}"></vxe-table-column>
                      <vxe-table-column field="rpt_itemname" title="菌属名称" width="180" :edit-render="{name:'input',autoselect: true}" sortable>
                      </vxe-table-column>
                      <!-- <vxe-table-column field="hospname" title="所属医院" width="180" sortable align="center" :edit-render="{name:'input',autoselect: true}">
                              </vxe-table-column> -->
                  </vxe-table>
              </template>
          </rm-layout-right>
      </div>
      <el-dialog title="提示" :visible.sync="havegroupVisible" width="30%">
          <span><span class="el-icon-erro"><b style="color:red">删除失败：</b>当前分类名称<b style="color:red">{{ message }}</b>下存在菌属数据。</span></span>
          <span slot="footer" class="dialog-footer">
              <el-button @click="havegroupVisible=false">确 定</el-button>
          </span>
      </el-dialog>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
          <span><span class="el-icon-info">确定删除分类名称为<b style="color:red">{{ message }}</b>的记录吗？</span></span>
          <span slot="footer" class="dialog-footer">
              <el-button @click="SubmitDel">确 定</el-button>
              <el-button @click="dialogVisible = false">取 消</el-button>
          </span>
      </el-dialog>
  </div>
</template>

<script type="text/javascript">
import rmLayoutRight from '../../../rmcompents/rm-layout-right.vue';
export default {
  components: { rmLayoutRight },
  name: "LIS810",
  data() {
      return {
          tableData: [],
          rXTableData:[],
          message: "",
          selectrow: [],
          germmedgroupOption: [],
          curselectrow: "",
          actionindex: 0,
          tempindex: 0,
          havegroupVisible: false,
          dialogVisible: false,
          okFirstLoading: true,
          keyConfig: {
              isArrow: true,
              isDel: true,
              isTab: true,
              isEdit: true,
          },
      };
  },
  created() {
      this.InitPageDate(); //初始化主列表
      this.ddlInitialList();
  },
  mounted() {},
  updated() {
      if (this.okFirstLoading) {
          let currentrow = "";
          if (this.actionindex != 0) {
              //新增，修改，删除
              if (this.actionindex == 3) {
                  //如果是删除
                  if (this.tempindex > 0) {
                      currentrow = this.$refs.xTable.getData(this.tempindex);
                  } else {
                      currentrow = this.$refs.xTable.getData(0);
                  }
              } else {
                  //新增或修改
                  if (this.curselectrow && this.curselectrow.germkind != "") {
                      //this.curselectrow
                      this.$nextTick(() => {
                          let row = this.curselectrow;
                          var alld = this.$refs.xTable.tableData;
                          for (var i = 0; i < alld.length; i++) {
                              if (alld[i].germkind == row.germkind) {
                                  this.tempindex = i;
                                  break;
                              }
                          }
                          currentrow = this.$refs.xTable.getData(this.tempindex);
                      });
                  } else {
                      currentrow = this.$refs.xTable.getData(0);
                  }
              }
          } else {
              //查询
              currentrow = this.$refs.xTable.getData(0);
              this.actionindex = 2;
              this.curselectrow = currentrow;
          }
          this.$nextTick(() => {
              if (currentrow) {
                  this.okFirstLoading = false;
                  this.$refs.xTable.setCurrentRow(currentrow);
                  this.message = currentrow.germkindname;
              }
          });
      }
  },
  methods: {
      InitPageDate() {
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS8/LIS810",
                  method: "post",
                  data: {
                      action: "query",
                  },
              })
              .then((res) => {
                  this.tableData = res.Data;

                  this.loading = false;
                  this.okFirstLoading = true;
              })
              .catch((e) => {
                  console.log("error", e);
              });
      },
      rowClassName({ row, rowIndex }) {
          let isDirty = this.$refs.xTable.hasRowChange(row);
          if (isDirty) {
              return "row-modify";
          }
          if (this.$refs.xTable.hasRowInsert(row)) return "row-new";
      },
      currentChangeEvent({ row, $rowIndex }) {
          this.message = row.germkindname;
          this.actionindex = 2; //修改
          this.tempindex = $rowIndex;
          this.curselectrow = row;
          this.detailQuery(row);
      },
      ddlInitialList() {
          this.$tool
              .request({
                  url: "LIS/LIS8/LIS810",
                  method: "post",
                  data: {
                      action: "ddlInitialList",
                  },
              })
              .then((response) => {
                  this.germmedgroupOption = response;
              });
      },
      async detailQuery(row) {
          try {
              this.rXTableData=[]
              const res = await this.$tool.request({
                  url: "LIS/LIS8/LIS810",
                  method: "post",
                  data: {
                      action: "querygermgroup",
                      germkind: row.germkind=""?'-1':row.germkind,
                  },
              })
              if (res.length > 0) {
                  this.rXTableData = res
                  this.$refs.rXTable.setCurrentRow(this.rXTableData[0])
              } else {
                  this.rXTableData = []
              }
          } catch (e) {
              this.$message.error(e)
          }
      },
      btnAdd() {
          let rXTableData=[]
          let initdata = {
              germkind: "",
              germkindname: "",
              kindgroup: "",
              kindname_en: "",
              germmedgroup: "",
              inputcode: "",
              seq: "",
              newrow:1,
          };
          this.$refs.xTable.insert(initdata);
          this.actionindex = 1; //增加
          this.FunscrollY();
          let rows = this.$refs.xTable.tableData;
          let selectrow = this.$refs.xTable.tableData[rows.length - 1];
          if (selectrow && selectrow != "") {
              this.$refs.xTable.setCurrentRow(selectrow);
              this.$refs.xTable.setActiveRow(selectrow, "germkind");
          }
      },
      FunscrollY() {
          this.$nextTick(() => {
              var alldataLen = this.$refs.xTable.tableData.length;
              let L = this.$refs.xTable.getTableData().fullData[alldataLen - 1];
              this.$refs.xTable.scrollToRow(L);
          });
      },
      btnDelete() {
          if(this.$refs.rXTable.tableData.length>0){
               this.havegroupVisible = true;
          }else{
              this.dialogVisible = true;
          }
      },
      SubmitDel() {
          let delrow = this.$refs.xTable.getCurrentRow();
          if (delrow) {
              var alld = this.$refs.xTable.tableData;
              var rowindex = alld.indexOf(delrow);
              this.$refs.xTable.remove(delrow);
              this.actionindex = 3; //删除
              let havedata = this.$refs.xTable.tableData;
              if (havedata.length > 0) {
                  if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                  this.tempindex = rowindex;
                  let selectrow = havedata[rowindex];
                  this.$refs.xTable.setCurrentRow(selectrow);
                  this.detailQuery(selectrow);
                  this.message = selectrow.germkindname;
              }
          } else {
              this.$message({
                  message: "请选择要删除的记录！",
                  type: "warning",
              });
          }
          this.dialogVisible = false;
      },
      EditChange({ row, columnIndex, rowIndex }) {
          let thisinputcode = "";
          let getpinyin = this.$pinyin(row.germkindname, {
              style: this.$pinyin.STYLE_FIRST_LETTER, // 首字母
              heteronym: true,
          });
          for (var i = 0; i < getpinyin.length; i++) {
              var curchar = getpinyin[i];
              if (curchar.length > 1) {
                  thisinputcode += curchar[0];
              } else {
                  thisinputcode += curchar;
              }
          }
          row.inputcode = thisinputcode.toUpperCase();
      },
      SaveRecord() {
          let tabledata = this.$refs.xTable.getRecordset();
          if (tabledata.insertRecords.length > 0) {
              this.curselectrow = tabledata.insertRecords[0];
          }
          let insertresult = [];
          let insertobj = tabledata.insertRecords;
          for (var i = 0; i < insertobj.length; i++) {
              var obj = insertobj[i];
              if (!!obj["germkind"]) {
                  insertresult.push(obj);
              }
          }
          tabledata.insertRecords = insertresult;
          this.$tool
              .request({
                  url: "LIS/LIS8/LIS810",
                  method: "post",
                  data: {
                      action: "save",
                      urec: tabledata,
                  },
              })
              .then((response) => {
                  if (response.errcode == "1") {
                      this.$message({
                          message: "操作成功！",
                          type: "success",
                      });
                      this.InitPageDate();
                  } else {
                      this.$message({
                          message: "保存失败！" + response.errmsg,
                          type: "warning",
                      });
                  }
              });
      },
      RecordExport() {
          this.$refs.xTable.exportCsv({ filename: "LIS810 细菌分类" });
      },
      CellActive({ row, columnIndex, column }) {
          if (row.germkind == null) {
              row.germkind = "";
              row.germkindname = "";
              row.kindgroup = "";
              row.kindname_en = "";
              row.germmedgroup = "";
              row.inputcode = "";
              row.seq = "";
          }
          this.message = row.germkindname;
          this.$refs.xTable.setCurrentRow(row);
          this.actionindex = 2; //修改
          this.tempindex = this.$refs.xTable.getRowIndex(row);
      },
      activeCellMethod({ column, columnIndex,row,rowIndex}){
          if(column.property =="germkind")
          {
              return row.newrow == 1;
          }else return true;
      },
      editDisabledEvent({row,column})
      {
           if(column.property =="germkind")
          {
             this.$message.info("分类代码禁止修改。")
          }       
      },
      cellClassName({ column,columnIndex, row }) {
          if (columnIndex == 0) {
              return this.$pub.diseditcolumn();
          }
          if(column.property =="germkind"&&row.newrow != 1)
          {
              return "Forbid";
          }
      },
  },
};
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 100%;
  left: 0px;
  right: 0px;
  overflow: auto;
}
/deep/.middle-center tr td div .el-input__inner {
  border: 1px solid rgba(255, 254, 254, 0) !important;
  background-color: rgba(255, 254, 254, 0) !important;
}
/deep/ .vxe-table .vxe-body--column.col-orange {
  background-color: #ccc;
}
/deep/ tr.vxe-body--row.row-modify {
  color: blue !important;
}
/deep/ tr.vxe-body--row.row-new {
  color: red !important;
}
/deep/.Forbid {
  background-color: #F8F8F8 !important;
}
</style>